package com.andox.fn.inf.rpc.rest;

import java.io.Serializable;

import com.andox.fn.inf.rpc.rest.exception.ServiceRuntimeException;
import com.andox.fn.inf.rpc.rest.serializer.Serializer;


/**   
 * @ClassName:   HttpResponse   
 * @Description: TODO  
 * @author:      Andox
 * @date:        2019年7月29日 下午1:29:15   
 *     
 */
public class RpcResponse implements Serializable{
	private static final long serialVersionUID = 8188317435220158433L;

	/**
	 *     序列化框架使用
	 */
	public RpcResponse() {
	}
	
	public RpcResponse(long opaque) {
		this.opaque = opaque;
		this.code = ResponseCodes.SUCCESS;
	}
	
	private long opaque;
	private String code;
	private String msg;
	private String data;
	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}
	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}
	/**
	 * @return the msg
	 */
	public String getMsg() {
		return msg;
	}
	/**
	 * @param msg the msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}
	/**
	 * @return the data
	 */
	public String getData() {
		return data;
	}
	/**
	 * @param data the data to set
	 */
	public void setData(String data) {
		this.data = data;
	}
	/**
	 * @return the opaque
	 */
	public long getOpaque() {
		return opaque;
	}
	/**
	 * @param opaque the opaque to set
	 */
	public void setOpaque(long opaque) {
		this.opaque = opaque;
	}
	/**
	 * @return the serialversionuid
	 */
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	public Object recreate(Serializer serializer) {
		if (ResponseCodes.SUCCESS.equals(this.code))
			return serializer.unserializeFromString(this.getData(), null);
		throw new ServiceRuntimeException(this.getMsg());
	}
	
	public Object recreate() {
		if (ResponseCodes.SUCCESS.equals(this.code))
			return this.getData();
		throw new ServiceRuntimeException(this.getMsg());
	}
	
	public static RpcResponse newInstance(long id, String code, String msg, String data) {
		RpcResponse ar = new RpcResponse(id);
		ar.setCode(code);
		ar.setMsg(msg);
		ar.setData(data);
		return ar;
	}
	
	public static RpcResponse success(long id, String data) {
		return RpcResponse.newInstance(id, ResponseCodes.SUCCESS, null, data);
	}
	
	public static RpcResponse failed(long id, String code, String msg) {
		return RpcResponse.newInstance(id, code, msg, null);
	}
}
